<?php

namespace common\models\datas;

use common\components\behaviors\UpdateCacheBehavior;
use Yii;
use yii\helpers\ArrayHelper;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use common\components\extenders\MenuManage;

/**
 * This is the model class for table "{{%integral_config_item}}".
 *
 * @property integer $id
 * @property integer $config_id
 * @property integer $site_id
 * @property string $name
 * @property string $formula
 * @property string $template
 * @property integer $admin_id
 * @property integer $created_at
 */
class IntegralConfigItem extends \common\components\bases\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%integral_config_item}}';
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        return $fields;
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['config_id', 'name', 'formula', 'template'], 'required'],
            [['config_id', 'site_id', 'admin_id', 'created_at'], 'integer'],
            [['name', 'formula', 'template'], 'string', 'max' => 255],
            [['config_id'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('common', '自动编号'),
            'config_id' => Yii::t('common', '配置编号'),
            'site_id' => Yii::t('common', '所属分店'),
            'name' => Yii::t('common', '积分标题'),
            'formula' => Yii::t('common', '公式'),
            'template' => Yii::t('common', '模板'),
            'admin_id' => Yii::t('common', '操作管理员'),
            'created_at' => Yii::t('common', '创建时间'),
        ];
    }

    public function extraFields()
    {
        return [
            'admin' => function () {
                return $this->admin->username;
            },
            'config_name' => function () {
                return $this->integralConfig->name;
            },
            'frontend_operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->create();
            },

        ];
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                ],
            ],
            [
                'class' => UpdateCacheBehavior::className(),
                'cacheName' => 'integralconfigitem'
            ]
        ]);
    }

    public function getAdmin()
    {
        return $this->hasOne(Admin::className(), ['id' => 'admin_id']);
    }

    public function getIntegralConfig()
    {
        return $this->hasOne(IntegralConfig::className(), ['id' => 'config_id']);
    }
}
